package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppTaskRecord;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppTaskRecordDao.java
 * @Description TODO
 * @createTime 2021年07月26日 12:19:00
 */
@Repository
public class AppTaskRecordDao extends BaseDao<AppTaskRecord> {


    public void saveOrUpdate(AppTaskRecord appTaskRecord) {
        if (appTaskRecord.getId() == null || appTaskRecord.getId() == 0) {
            appTaskRecord.setId(this.add(appTaskRecord));
        } else {
            this.update(appTaskRecord);
        }

    }

    public List<AppTaskRecord> findList(Map<String, Object> paramap, SplitPageBean splitPageBean) {
        List<Object> list = new ArrayList<>();
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_task_record where 1=1 ");
        if (StringEx.isNotNull(paramap.get("userid"))) {
            str.append(" and userid = ? ");
            str.append(" and createtime >= DATE_FORMAT(now(), '%y-%m-%d 00:00:00') and createtime <= DATE_FORMAT(now(),'%y-%m-%d 23:59:59')");
            list.add(paramap.get("userid"));
        }
        String sql = str.toString();
        return this.queryList(sql, list, AppTaskRecord.class, splitPageBean);
    }

    //根据任务id和用户id获取数据
    public AppTaskRecord getByTaskId(Integer userid, Integer taskid) {

        List<Object> list = new ArrayList<>();
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_task_record where userid = ? and  taskid = ? ");
        list.add(userid);
        list.add(taskid);
        String sql = str.toString();
        return this.queryObj(sql, list, AppTaskRecord.class);

    }


    public List<AppTaskRecord> list(Map<String, Object> paramap) {
        List<Object> list = new ArrayList<>();
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_task_record where lingqu=3 ");
        if(paramap.containsKey("today") && StringEx.isNotNull(paramap.get("today"))) {
            str.append(" and to_days(createtime) = to_days(now())");
        }
        String sql = str.toString();
        return this.queryList(sql, list, AppTaskRecord.class);
    }

    public Boolean isCompleteToday(Integer userId, String typeNameEn) {
        List<Object> list = new ArrayList<>();
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_task_record where to_days(createtime) = to_days(now()) ");
        str.append(" and userid = ? and taskname=?");

        list.add(userId);
        list.add(typeNameEn);
        String sql = str.toString();
        List<AppTaskRecord> appTaskRecords = this.queryList(sql, list, AppTaskRecord.class);
        return appTaskRecords.size() > 0;
    }

    public Boolean isCompleteOnce(Integer userId, String typeNameEn) {
        List<Object> list = new ArrayList<>();
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_task_record where 1=1 ");
        str.append(" and userid = ? and taskname=?");

        list.add(userId);
        list.add(typeNameEn);
        String sql = str.toString();
        List<AppTaskRecord> appTaskRecords = this.queryList(sql, list, AppTaskRecord.class);
        return appTaskRecords.size() > 0;
    }
}
